package cn.com.anypay.manager.miaoma.manual.providerchannelconfig;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import jakarta.validation.Valid;
import java.util.HashMap;
import java.util.Map;

/**
 * 服务商渠道配置控制器
 */
@RestController
@RequestMapping("/api/v1/miaoma/manual/provider-channel-config")
public class ProviderChannelConfigController {

    @Autowired
    private ProviderChannelConfigService providerChannelConfigService;

    /**
     * 分页查询服务商渠道配置列表
     */
    @GetMapping("/list")
    public Map<String, Object> list(
            @RequestParam(defaultValue = "1") Integer page,
            @RequestParam(defaultValue = "10") Integer size,
            @RequestParam(required = false) Long providerId,
            @RequestParam(required = false) Long channelId,
            @RequestParam(required = false) Integer status) {

        Page<ProviderChannelConfig> pageParam = new Page<>(page, size);
        IPage<ProviderChannelConfig> result = providerChannelConfigService.queryByPage(pageParam, providerId, channelId, status);

        Map<String, Object> response = new HashMap<>();
        response.put("success", true);
        response.put("data", result.getRecords());
        response.put("total", result.getTotal());
        response.put("current", result.getCurrent());
        response.put("size", result.getSize());

        return response;
    }

    /**
     * 根据ID获取服务商渠道配置详情
     */
    @GetMapping("/{id}")
    public Map<String, Object> getById(@PathVariable Long id) {
        Map<String, Object> response = new HashMap<>();

        ProviderChannelConfig config = providerChannelConfigService.getById(id);
        if (config != null) {
            response.put("success", true);
            response.put("data", config);
        } else {
            response.put("success", false);
            response.put("message", "配置不存在");
        }

        return response;
    }

    /**
     * 添加服务商渠道配置
     */
    @PostMapping("/save")
    public Map<String, Object> save(@Valid @RequestBody ProviderChannelConfig config) {
        Map<String, Object> response = new HashMap<>();

        boolean success = providerChannelConfigService.saveOrUpdateConfig(config);
        response.put("success", success);
        response.put("message", success ? "保存成功" : "保存失败");

        return response;
    }

    /**
     * 更新服务商渠道配置
     */
    @PostMapping("/update")
    public Map<String, Object> update(@Valid @RequestBody ProviderChannelConfig config) {
        Map<String, Object> response = new HashMap<>();

        if (config.getId() == null) {
            response.put("success", false);
            response.put("message", "ID不能为空");
            return response;
        }

        boolean success = providerChannelConfigService.saveOrUpdateConfig(config);
        response.put("success", success);
        response.put("message", success ? "更新成功" : "更新失败");

        return response;
    }

    /**
     * 删除服务商渠道配置
     */
    @PostMapping("/delete/{id}")
    public Map<String, Object> delete(@PathVariable Long id) {
        Map<String, Object> response = new HashMap<>();

        boolean success = providerChannelConfigService.removeById(id);
        response.put("success", success);
        response.put("message", success ? "删除成功" : "删除失败");

        return response;
    }

    /**
     * 批量删除服务商渠道配置
     */
    @PostMapping("/batchDelete")
    public Map<String, Object> batchDelete(@RequestBody java.util.List<Long> ids) {
        Map<String, Object> response = new HashMap<>();

        if (ids == null || ids.isEmpty()) {
            response.put("success", false);
            response.put("message", "请选择要删除的记录");
            return response;
        }

        boolean success = providerChannelConfigService.removeByIds(ids);
        response.put("success", success);
        response.put("message", success ? "批量删除成功" : "批量删除失败");

        return response;
    }

    /**
     * 更新配置状态
     */
    @PostMapping("/updateStatus")
    public Map<String, Object> updateStatus(@RequestParam Long id, @RequestParam Integer status) {
        Map<String, Object> response = new HashMap<>();

        ProviderChannelConfig config = providerChannelConfigService.getById(id);
        if (config == null) {
            response.put("success", false);
            response.put("message", "配置不存在");
            return response;
        }

        config.setStatus(status);
        boolean success = providerChannelConfigService.saveOrUpdateConfig(config);
        response.put("success", success);
        response.put("message", success ? "状态更新成功" : "状态更新失败");

        return response;
    }
}